package io.lvdaxian.mybatis13.mapping;

import io.lvdaxian.mybatis13.session.Configuration;

import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

/**
 * 结果映射 类
 *
 * @author lihh
 */
public class ResultMap {
  // 对应的片段 id
  private String id;
  // 对应的返回类型
  private Class<?> type;
  // 表示结果映射集合
  private List<ResultMapping> resultMappings;
  // 需要返回的类列
  private Set<String> mappedColumns;
  
  private ResultMap() {
  }
  
  public static class Builder {
    private ResultMap resultMap = new ResultMap();
    
    public Builder(Configuration configuration, String id, Class<?> type, List<ResultMapping> resultMappings) {
      resultMap.id = id;
      resultMap.type = type;
      resultMap.resultMappings = resultMappings;
    }
    
    public ResultMap build() {
      resultMap.mappedColumns = new HashSet<>();
      // 添加 mappedColumns 字段
      for (ResultMapping resultMapping : resultMap.resultMappings) {
        final String column = resultMapping.getColumn();
        if (column != null) {
          resultMap.mappedColumns.add(column.toUpperCase(Locale.ENGLISH));
        }
      }
      return resultMap;
    }
    
  }
  
  public String getId() {
    return id;
  }
  
  public Set<String> getMappedColumns() {
    return mappedColumns;
  }
  
  public Class<?> getType() {
    return type;
  }
  
  public List<ResultMapping> getResultMappings() {
    return resultMappings;
  }
  
  public List<ResultMapping> getPropertyResultMappings() {
    return resultMappings;
  }
}
